
@echo off
if not "%cd0%"=="" goto end_init
set cd0=%cd%
call vcvars64
@echo on
:end_init

@echo off
if exist demo goto end_clone
@echo on
call git clone https://github.com/versatica/mediasoup-demo demo -n
@echo off
if not %errorlevel%==0 goto end
cd demo
@echo on
git config core.autocrlf input
git checkout
:end_clone

@echo off
cd %cd0%\demo\server
if exist node_modules\mediasoup\package.json goto end_i_mediasoup
@echo on
call npm i mediasoup --ignore-scripts
@echo off & if not %errorlevel%==0 goto end
:end_i_mediasoup
@echo on
call npm i
@echo off & if not %errorlevel%==0 goto end

@echo off
set mediasoup=%cd0%\demo\server\node_modules\mediasoup
set build=%mediasoup%\worker\out\release\build
if exist %build% (
    echo Clearing "%build%"
    rd /s /q %build%
)
cd %mediasoup%\worker\subprojects
for /f "delims=" %%f in ('dir /b /ad-h-s') do (
    if "%%f"=="packagecache" (
        set _tmp=0
    ) else if "%%f"=="packagefiles" (
        set _tmp=0
    ) else if not exist %%f\meson.build (
        echo Clearing subproject "%%f" due to lack of "meson.build"
        rd /s /q %%f
    )
)
cd %mediasoup%
@echo on
call node npm-scripts.js postinstall
@echo off & if not %errorlevel%==0 goto end

@echo off & cd %cd0%\demo\app & @echo on
call npm i
@echo off & if not %errorlevel%==0 goto end

:end
echo;
@pause
@echo off & cd %cd0% & @echo on